package com.kun.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.kun.servicebase.exceptionhandler.KunException;
import com.kun.utils.R;
import com.kun.vod.Utils.VodUtils;
import com.kun.vod.Utils.vod;
import com.kun.vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/eduVod/video")
@CrossOrigin
public class VodController {
    @Autowired
    private VodService vodService;
    @PostMapping("/uploadVideo")
    public R uploadVideo(MultipartFile file){
        String videoId= vodService.uploadVideo(file);
        return R.ok().data("videoId",videoId);
    }
    @DeleteMapping("/delvod/{id}")
    public R delVideo(@PathVariable("id") String id){
        vodService.delVideo(id);
        return R.ok();
    }
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeMoreAlyVideo(videoIdList);
        return R.ok();
    }
    @GetMapping("getPlayerById/{id}")
    public R getPlayerById(@PathVariable String id){
        try {
            DefaultAcsClient client = vod.initVodClient(VodUtils.KEY_ID, VodUtils.KET_SECRET);

            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            request.setVideoId(id);
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            System.out.println(playAuth);
            return R.ok().data("playAuth",playAuth);
        }catch (Exception e){

            throw new KunException(20001,"获取凭证失败");

        }
    }
}
